package org.phalanx.client.UI;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.phalanx.client.UI.swing.pComponent;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;

public class PhalanxIconButton extends JLabel implements MouseListener, pComponent{
	String phalanxText;
	private Hashtable<Integer, Icon> iconTable = new Hashtable<Integer, Icon>();  
	public static final int DEFAULT_ICON = 0;
	public static final int CLICKED_ICON = 1;
	public static final int HOVERED_ICON = 2;
	private static final int TEXT_ALIGNMENT = SwingConstants.BOTTOM;
	private ArrayList<pButtonListener> listenerTable = new ArrayList<pButtonListener>();
	
	public PhalanxIconButton(){
		init();
	}
	
	public PhalanxIconButton(Icon defaultIcon){
		setDefaultIcon(defaultIcon);
		init();
	}
	
	public PhalanxIconButton(Icon defaultIcon, String caption){
		this.setDefaultIcon(defaultIcon);
		this.setText(caption);
		init();
	}
	
	public void setDefaultIcon(Icon newIcon){
		iconTable.put(DEFAULT_ICON, newIcon);
		this.setIcon(newIcon);
	}
	
	public void setClickedIcon(Icon newIcon){
		iconTable.put(CLICKED_ICON, newIcon);
	}
	
	public void setHoveredIcon(Icon newIcon){
		iconTable.put(HOVERED_ICON, newIcon);
	}
	
	public Icon getIcon(int iconType){
		return iconTable.get(iconType);
	}
	
	private void init(){
		this.addMouseListener(this);
	}
	
	

	@Override
	public void setEnabled(boolean enabled) {
		if (enabled){
			if (!this.isEnabled()){
				this.addMouseListener(this);
			}
		}else{
			this.removeMouseListener(this);
		}
		super.setEnabled(enabled);
	}

	@Override
	public void setText(String text) {
		this.setVerticalTextPosition(TEXT_ALIGNMENT);
		this.setHorizontalTextPosition(SwingConstants.CENTER);
		super.setText(text);
	}

	public void mouseClicked(MouseEvent arg0) {		
	}

	public void mouseEntered(MouseEvent arg0) {
		this.setIcon(iconTable.get(HOVERED_ICON));
	}

	public void mouseExited(MouseEvent arg0) {
		this.setIcon(iconTable.get(DEFAULT_ICON));
	}

	public void mousePressed(MouseEvent arg0) {
		if (arg0.getButton() == MouseEvent.BUTTON1){
			this.setIcon(iconTable.get(CLICKED_ICON));
		}
	}

	public void mouseReleased(MouseEvent arg0) {
		if (arg0.getButton() == MouseEvent.BUTTON1){
			Integer mousePosX = arg0.getX();
			Integer mousePosY = arg0.getY();

			if (mousePosX > this.getWidth() || mousePosY > this.getHeight() 
					|| mousePosX < 0 || mousePosY < 0){
				this.setIcon(iconTable.get(DEFAULT_ICON));
			} else {
				this.setIcon(iconTable.get(HOVERED_ICON));
				activateListeners();
			}
		}
	}
	
	private void activateListeners(){
		pButtonPressed thisAction = new pButtonPressed(this);
		for (int i = 0; i < listenerTable.size(); i++){
			pButtonListener tempListener = listenerTable.get(i);
			tempListener.actionPerformed(thisAction);
		}
	}
	public void addButtonListener(pButtonListener l){
		if (l != null){
			listenerTable.add(l);
		}
	}
	
	public void removeButtonListener(pButtonListener l){
		if (l != null){
			listenerTable.remove(l);
		}
	}
	
	public void clearButtonListeners(){
		listenerTable.clear();
	}
}
